﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:IkeCode.WP7.HQReader.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
*/

using System;
using System.Diagnostics.CodeAnalysis;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using IkeCode.WP7.HQReader.Interfaces;
using IkeCode.WP7.HQReader.Services;
using IkeCode.WP7.HQReader.Services.Design;
using IkeCode.WP7.Toolkit.Base.Navigation;
using IkeCode.WP7.Toolkit.Persist;
using Microsoft.Practices.ServiceLocation;
using IkeCode.WP7.HQReader.Model;

namespace IkeCode.WP7.HQReader.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator
    {
        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            var isInDesign = ViewModelBase.IsInDesignModeStatic;
            var createInstance = !isInDesign;

            if (isInDesign)
            {
                SimpleIoc.Default.Register<IFeedDataService, DesignFeedDataService>();
                SimpleIoc.Default.Register<IFeedPublisherDataService, DesignFeedPublisherDataService>();
            }
            else
            {
                SimpleIoc.Default.Register<IFeedDataService, FeedDataService>();
                SimpleIoc.Default.Register<IFeedPublisherDataService, FeedPublisherDataService>();
            }

            SimpleIoc.Default.Register<IcSmartPersistService>(()=> new IcSmartPersistService(), createInstance);
            
            SimpleIoc.Default.Register<INavigationService, NavigationService>();
            SimpleIoc.Default.Register<MainViewModel>();
            SimpleIoc.Default.Register<FeedViewModel>();
            SimpleIoc.Default.Register<PublisherManagerViewModel>();
            SimpleIoc.Default.Register<FeedDetailViewModel>();
            SimpleIoc.Default.Register<IcLoadingService>();
        }

        /// <summary>
        /// Main View Model
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

        /// <summary>
        /// Feed View Model
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
        public FeedViewModel Feed
        {
            get
            {
                return ServiceLocator.Current.GetInstance<FeedViewModel>();
            }
        }

        /// <summary>
        /// Publisher Manager View Model
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
        public PublisherManagerViewModel PublisherManager
        {
            get
            {
                return ServiceLocator.Current.GetInstance<PublisherManagerViewModel>();
            }
        }

        /// <summary>
        /// Feed Detail View Model
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
        public FeedDetailViewModel FeedDetail
        {
            get
            {
                return ServiceLocator.Current.GetInstance<FeedDetailViewModel>();
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
        }
    }
}